package com.walker.weixin.controller;

import com.walker.weixin.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.weixin.controller
 * @ClassName: WxController
 * @Author: yangyl
 * @CreateDate: 2020/7/12 23:26
 */

@Controller
@RequestMapping("/wx")
public class WxController {

    @Autowired
    protected WxService wxService;

    @GetMapping("init")
    public void getWxMessage(HttpServletRequest request, HttpServletResponse response){
        String signature=request.getParameter("signature");
        String timestamp=request.getParameter("timestamp");
        String nonce=request.getParameter("nonce");
        String echostr=request.getParameter("echostr");
        String check = wxService.check(signature, timestamp, nonce, echostr);
        writeResponse(response,check);
    }

    private void writeResponse(HttpServletResponse response,String data){
        try {
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print(data);
            writer.flush();
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @PostMapping("init")
    public void postWxMessage(HttpServletRequest request, HttpServletResponse response){
        System.out.println("post");
    }

}
